package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.WeeklyDataBean;
import com.lzx.iteam.bean.WeeklyDetailListData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Megan on 16/5/25.
 */
public class WeeklyMyListMsg extends CcMsgStructure {

    private static final String TAG = "WeeklyMyListMsg";

    private Message mmCallback;
    private Context mContext;
    private String mGroupId;
    private ArrayList<WeeklyDetailListData> weeklyDetailListDatas;
    private CloudDBOperation cloudDBOperation;
    private String mAction;
    private String mYear,mMonth,mWeekCount;


    public WeeklyMyListMsg(Message callback, Context context, String groupId, String action) {
        mmCallback = callback;
        mContext = context;
        mGroupId = groupId;
        mAction = action;
        cloudDBOperation = new CloudDBOperation(mContext);
    }

    public WeeklyMyListMsg(Message callback, Context context, String groupId, String action,String year,String month,String weekCount) {
        mmCallback = callback;
        mContext = context;
        mGroupId = groupId;
        mAction = action;
        mYear=year;
        mMonth=month;
        mWeekCount=weekCount;
        cloudDBOperation = new CloudDBOperation(mContext);

    }


    @Override
    public void onReceive(JSONObject data) {
        weeklyDetailListDatas = new ArrayList<WeeklyDetailListData>();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray jsonArray = null;
            if (jsonObject.has("list")) {
                jsonArray = jsonObject.getJSONArray("list");
            } else if (jsonObject.has("details")) {
                jsonArray = jsonObject.getJSONArray("details");
            }
            if("my_weekly".equals(mAction)){
                cloudDBOperation.deleteAllWeeklyDetailList(mGroupId, mAction);
            }else if("weekly_list".equals(mAction)){
                cloudDBOperation.deleteAllThisWeeklyDetailList(mGroupId, mYear, mMonth, mWeekCount, mAction);
            }
            for (int i = 0; i < jsonArray.length(); i++) {
                WeeklyDetailListData weeklyDetailListData = new WeeklyDetailListData();
                JSONObject item = jsonArray.getJSONObject(i);
                weeklyDetailListData.groupId = mGroupId;
                if("my_weekly".equals(mAction)){
                    weeklyDetailListData.userId= PreferenceUtil.getInstance(mContext).getString(PreferenceUtil.CLIENT_USEID, "－1");
                    ArrayList<String> nameCard = (ArrayList<String>) PreferenceUtil.getInstance(mContext).getCloudContact(Constants.CONTACT_NAME_CARD);
                    if (nameCard != null && nameCard.size() > 0) {
                        weeklyDetailListData.userName = nameCard.get(0);
                        weeklyDetailListData.userImg = nameCard.get(7);
                    }
                }
                if (item.has("weekly_year")) {
                    weeklyDetailListData.weeklyYear = item.getString("weekly_year");
                }
                if (item.has("weekly_month")) {
                    weeklyDetailListData.weeklyMonth = item.getString("weekly_month");
                }
                if (item.has("week_count")) {
                    weeklyDetailListData.weeklyCount = item.getString("week_count");
                }
                if(item.has("user_id")){
                    weeklyDetailListData.userId = item.getString("user_id");
                }
                if(item.has("user_name")){
                    weeklyDetailListData.userName = item.getString("user_name");
                }

                if(item.has("user_img")){
                    weeklyDetailListData.userImg = item.getString("user_img");
                }

                weeklyDetailListData.createTime = item.getString("create_time");
                weeklyDetailListData.weeklyId = item.getString("_id");
                weeklyDetailListData.content = item.getJSONArray("content").toString();
                if (item.has("comments")) {
                    ArrayList<Map<String, String>> commentDatas = new ArrayList<Map<String, String>>();
                    JSONArray comments = item.getJSONArray("comments");
                    for (int j = 0; j < comments.length(); j++) {
                        JSONObject commentObject = comments.getJSONObject(j);
                        Map<String, String> comment = new HashMap<String, String>();
                        comment.put(Constants.COMMENT_CONTENT,commentObject.getString("comment_content"));
                        comment.put(Constants.COMMENT_CREATE_TIME,commentObject.getString("comment_create_time"));
                        comment.put(Constants.COMMENT_USER_ID,commentObject.getString("comment_user_id"));
                        comment.put(Constants.COMMENT_USER_NAME,commentObject.getString("comment_user_name"));
                        commentDatas.add(comment);
                    }
                    weeklyDetailListData.comments=commentDatas;
                }
                weeklyDetailListDatas.add(weeklyDetailListData);
                cloudDBOperation.insertWeeklyDetailListData(mAction, weeklyDetailListData);
            }
        } catch (JSONException e) {
            Log.d(TAG, "JSONException : " + e.toString());
            e.printStackTrace();
        }

        if (mmCallback != null)

        {
            mmCallback.obj = weeklyDetailListDatas;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
